package createThread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

class callableTest implements Callable{

    @Override
    public Object call() throws Exception {
        int i = 0;
        while (i++<5){
            System.out.println(Thread.currentThread().getName()
                    +"的call()方法被执行了！");
        }
        return i;
    }
}


public class callableThread {

    public static void main(String[] args) throws InterruptedException, ExecutionException {

        // 创建Callable接口实现类的实例对象
        callableTest callable = new callableTest();
        // 使用FutureTask封装接口实现类的实例对象
        FutureTask<Object> ft1 = new FutureTask<>(callable);
        // 使用Thread有参构造方法创建线程对象
        Thread thread1 = new Thread(ft1,"thread1");
        // 启动线程
        thread1.start();

        // 创建另一个线程对象
        // 使用FutureTask封装接口实现类的实例对象
        FutureTask<Object> ft2 = new FutureTask<>(callable);
        // 使用Thread有参构造方法创建线程对象
        Thread thread2 = new Thread(ft2,"thread2");
        // 启动线程
        thread2.start();

        System.out.println("thread1的返回结果："+ft1.get()+"个");
        System.out.println("thread2的返回结果："+ft2.get()+"个");
    }
}
